package com.solidumgame.ninja.pot
{
	
	import com.greensock.TweenMax;
	import com.pblabs.engine.PBE;
	import com.pblabs.engine.entity.IEntity;
	import com.solidumgame.ninja.pot.events.PotGenericEvents;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	
	public class Pot extends EventDispatcher
	{
		
		public static const TYPE:String = "POT";
		
		private var spatialComp:PotSpatialComponent;
		private var upRenderer:PotRendererComponent;
		private var renderer:PotRendererComponent;
		private var logic:PotLogicComponent;
		private var sound:PotSoundComponent;
		private var myEntity:IEntity;
		
		
		public function Pot()
		{
			spatialComp = new PotSpatialComponent();
			upRenderer = new PotRendererComponent();
			renderer = new PotRendererComponent();
			logic = new PotLogicComponent();
			sound = new PotSoundComponent();
			
			
			// Allocate new entity and add components.
			myEntity = PBE.allocateEntity();
			myEntity.addComponent(spatialComp, "Spatial");
			myEntity.addComponent(upRenderer,"UpRenderer");
			myEntity.addComponent(renderer, "Renderer");
			myEntity.addComponent(logic, "Logic");
			myEntity.addComponent(sound, "Sound");
			myEntity.initialize("POT");
			
			myEntity.eventDispatcher.addEventListener(PotGenericEvents.POT_EXIT_EVENT, onTableWoodExit);
			myEntity.eventDispatcher.addEventListener(PotGenericEvents.POT_ENTER_EVENT, onTableWoodEnter);
			
			logic.throwInTableWood();
			
		}
			
			
			protected function onTableWoodExit(e:PotGenericEvents):void{
				dispatchEvent(new PotGenericEvents(PotGenericEvents.POT_EXIT_EVENT));
			}
			
			
			protected function onTableWoodEnter(e:PotGenericEvents):void{
				dispatchEvent(new PotGenericEvents(PotGenericEvents.POT_ENTER_EVENT));
			}
			
			public function throwOutTableWood():void{
				logic.throwOutTableWood();
			}
			
			public function remove():void{
				myEntity.destroy();
				
				sound = null;
				myEntity = null;
				spatialComp = null;
				logic = null;
				renderer = null;
			}
			
		}
	}